package com.example.datagramsocket.base

import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import java.lang.reflect.ParameterizedType

/**
 * @author 宾有为
 * @date 2024/6/7 16:49
 * @link https://blog.csdn.net/baidu_41616022?type=blog
 * @describe：
 **/
abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {
    lateinit var mBinding: VB

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val type = javaClass.genericSuperclass
        val vbClass: Class<VB> = type!!.saveAs<ParameterizedType>().actualTypeArguments[0].saveAs()
        val method = vbClass.getDeclaredMethod("inflate", LayoutInflater::class.java)
        mBinding = method.invoke(this, layoutInflater)!!.saveAsUnChecked()
        setContentView(mBinding.root)
        initView()
    }

    inline fun <reified T> Any.saveAs() : T{
        return this as T
    }

    @Suppress("UNCHECKED_CAST")
    fun <T> Any.saveAsUnChecked() : T{
        return this as T
    }

    abstract fun initView()
}